{% extends "admin/base.html" %}

{% block title %}文章管理{% endblock %}
{% block page_title %}文章管理{% endblock %}

{% block page_actions %}
<a href="{{ url_for('admin.new_post') }}" class="btn btn-primary">
    <i class="fas fa-plus"></i> 写新文章
</a>
{% endblock %}

{% block content %}
<div class="card shadow">
    <div class="card-body">
        {% if posts.items %}
        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>标题</th>
                        <th>作者</th>
                        <th>分类</th>
                        <th>状态</th>
                        <th>发布时间</th>
                        <th>浏览次数</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for post in posts.items %}
                    <tr>
                        <td>
                            {% if post.is_published %}
                            <a href="{{ url_for('main.post_detail', slug=post.slug) }}" 
                               class="text-decoration-none" target="_blank">
                                {{ post.title }}
                            </a>
                            {% else %}
                            <a href="{{ url_for('main.preview_post', slug=post.slug) }}" 
                               class="text-decoration-none" target="_blank">
                                {{ post.title }}
                            </a>
                            <span class="badge bg-secondary ms-1">草稿</span>
                            {% endif %}
                            {% if post.is_featured %}
                            <span class="badge bg-warning text-dark ms-1">推荐</span>
                            {% endif %}
                        </td>
                        <td>{{ post.author.username }}</td>
                        <td>
                            {% if post.category %}
                            <a href="{{ url_for('main.category_posts', slug=post.category.slug) }}" 
                               class="text-decoration-none" target="_blank">
                                {{ post.category.name }}
                            </a>
                            {% else %}
                            <span class="text-muted">未分类</span>
                            {% endif %}
                        </td>
                        <td>
                            {% if post.is_published %}
                            <span class="badge bg-success">已发布</span>
                            {% else %}
                            <span class="badge bg-secondary">草稿</span>
                            {% endif %}
                        </td>
                        <td>{{ post.created_at.strftime('%Y-%m-%d %H:%M') }}</td>
                        <td>{{ post.view_count }}</td>
                        <td>
                            <div class="btn-group btn-group-sm" role="group">
                                <a href="{{ url_for('admin.edit_post', id=post.id) }}" 
                                   class="btn btn-outline-primary" title="编辑">
                                    <i class="fas fa-edit"></i>
                                </a>
                                {% if post.is_published %}
                                <a href="{{ url_for('main.post_detail', slug=post.slug) }}" 
                                   class="btn btn-outline-info" target="_blank" title="查看">
                                    <i class="fas fa-eye"></i>
                                </a>
                                {% else %}
                                <a href="{{ url_for('main.preview_post', slug=post.slug) }}" 
                                   class="btn btn-outline-info" target="_blank" title="预览">
                                    <i class="fas fa-eye"></i>
                                </a>
                                {% endif %}
                                <button type="button" class="btn btn-outline-danger" 
                                        onclick="deletePost({{ post.id }})" title="删除">
                                    <i class="fas fa-trash"></i>
                                </button>
                            </div>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>

        <!-- Pagination -->
        {% if posts.pages > 1 %}
        <nav aria-label="文章分页">
            <ul class="pagination justify-content-center">
                {% if posts.has_prev %}
                <li class="page-item">
                    <a class="page-link" href="{{ url_for('admin.posts', page=posts.prev_num) }}">上一页</a>
                </li>
                {% endif %}
                
                {% for page_num in posts.iter_pages() %}
                    {% if page_num %}
                        {% if page_num != posts.page %}
                        <li class="page-item">
                            <a class="page-link" href="{{ url_for('admin.posts', page=page_num) }}">{{ page_num }}</a>
                        </li>
                        {% else %}
                        <li class="page-item active">
                            <span class="page-link">{{ page_num }}</span>
                        </li>
                        {% endif %}
                    {% else %}
                    <li class="page-item disabled">
                        <span class="page-link">...</span>
                    </li>
                    {% endif %}
                {% endfor %}
                
                {% if posts.has_next %}
                <li class="page-item">
                    <a class="page-link" href="{{ url_for('admin.posts', page=posts.next_num) }}">下一页</a>
                </li>
                {% endif %}
            </ul>
        </nav>
        {% endif %}
        {% else %}
        <div class="text-center py-5">
            <i class="fas fa-file-alt fa-3x text-muted mb-3"></i>
            <h4 class="text-muted">还没有文章</h4>
            <p class="text-muted">开始写你的第一篇文章吧！</p>
            <a href="{{ url_for('admin.new_post') }}" class="btn btn-primary">
                <i class="fas fa-plus"></i> 写新文章
            </a>
        </div>
        {% endif %}
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
function deletePost(postId) {
    if (confirm('确定要删除这篇文章吗？此操作不可恢复！')) {
        const form = document.createElement('form');
        form.method = 'POST';
        form.action = `/admin/posts/${postId}/delete`;
        document.body.appendChild(form);
        form.submit();
    }
}
</script>
{% endblock %}

